/*
 * Copyright 2011 Marcos Fabio Pereira
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.moveme.network.payload;

import java.nio.ByteBuffer;

import com.moveme.network.payload.cameraslice.PSCameraFrameSlice;
import com.moveme.network.payload.state.PSMoveState;

/**
 * The payload class.
 * 
 * @author Marcos Fabio Pereira
 * 
 */
public abstract class PSPayload {

    /**
     * Payload header.
     */
    public PSMoveServerPacketHeader header;

    /**
     * Create a payload instance according to the payload type.
     * 
     * @param codeType
     *            The payload type. (0x1, 0x2 or 0x3).
     * @return A new payload instance. One of {@link PSCameraFrameSlice} (type
     *         0x2) or {@link PSMoveState} (types 0x1 and 0x3).
     */
    public static PSPayload createInstance(int codeType) {
        PSPayload payload = null;
        switch (codeType) {
        case PSMoveServerPacketHeader.STANDARD_STATE_CODE:
            payload = new PSMoveState();
            break;
        case PSMoveServerPacketHeader.CAMERA_FRAME_CODE:
            payload = new PSCameraFrameSlice();
            break;
        case PSMoveServerPacketHeader.CAMERA_STATE_CODE:
            payload = new PSMoveState();
            break;
        }
        return payload;
    }

    /**
     * Set the header of payload.
     * 
     * @param header
     *            The header.
     */
    public void setHeader(PSMoveServerPacketHeader header) {
        this.header = header;
    }

    /**
     * Each payload type (Camera Slice, Camera State and Standard State) has to
     * process the payload body on different ways.
     * 
     * @param byteBuffer
     *            The byteBuffer instance.
     */
    public abstract void processBody(ByteBuffer byteBuffer);

}
